/*
Purpose: Generate arrest rate table (Table 3)
*/

global sd92 = 36.3153155648024

*reading in state naep scores
use "data/clean/naep_data.dta", clear
keep fips yob math08sd92_gap1
statastates ,fips(fips)
drop if _merge==2
drop _merge
rename state_abbrev state
save "data/clean/statenaepscores",replace

use "data/clean/state_year_indices", clear //this is the data generated with NAEP microdata that includes adjusted scores

tsset fips year
tsfill


foreach var of varlist state_year_adj_score  { //imputing missing years if gap is only two years long
	gen `var'_gap1=`var'
	replace `var'_gap1=(L.`var'+F.`var')/2 if L.`var'!=.&F.`var'!=.
}

gen yob = year - 13

keep fips yob *_gap1
drop if state_year_adj_score_gap1==.

tempfile backgroundindex
save `backgroundindex', replace

*reading in donahue data
use "data/raw/arrests by state year and yob.dta", clear
destring l_arrs_cap_vio, replace force
destring l_arrs_cap_pro, replace force

gen yob=relevant_yob

merge m:1 fips yob using statenaepscores

*not using dc or those born before 1987...otherwise perfect match
drop if _merge==1|_merge==2
drop if fips==11
drop _merge

merge m:1 fips yob using `backgroundindex'

drop _merge

ren fips fips_birth

merge m:1 fips_birth yob using "data/clean/state_year_ses_index" //bringing in parental education variables
keep if _merge==3
drop _merge

ren fips_birth fips


recode fips (9 23 25 33 44 50 =1 "New England") (34 36 42 =2 "Mid Atlantic") (17 18 26 39 55 =3 "E North Central")  (19 20 27 29 31 38 46 = 4 "W North Central") (10 11 12 13 24 37 45 51 54 =5 "South Atlantic") (1 21 28 47 =6 "E South Central") (5 22 40 48 =7 "W South Central")  (4 8 16 30 32 35 49 56 =8 "Mountain")  (2 6 15 41 53 = 9 "Pacific"),gen(division_birth)

capture drop fips_yob
egen fips_yob=group(fips yob)

replace state_year_adj_score_gap1 = (state_year_adj_score_gap1) / $sd92

eststo clear
//running our main specification on violent and property crime
eststo vio: quietly reg l_arrs_cap_vio state_year_adj_score_gap1 norm_either_par_gcol_2yr division_birth#yob age#year i.fips [pw=seer_pop] ,robust cluster(fips_yob)
eststo pro: quietly reg l_arrs_cap_pro state_year_adj_score_gap1 norm_either_par_gcol_2yr division_birth#yob age#year i.fips [pw=seer_pop] ,robust cluster(fips_yob)
esttab using "results/arrests and scores",  mlabel("violent crimes" "property crimes") csv cells(b(star fmt(3))  se(par fmt(3))) replace label order(state_year_adj_score_gap1 norm_either_par_gcol_2yr ab_res_agi_rate_fg)